package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api("管理端订单相关接口")
@Slf4j
@RestController("adminOrdersController")
@RequestMapping("/admin/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;


    /**
     * 获取所有订单
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation("获取所有订单")
    @GetMapping("/conditionSearch")
    public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("商家获取所有订单:{}", ordersPageQueryDTO);
        PageResult pageResult = ordersService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 获取订单详情
     * @param id
     * @return
     */
    @ApiOperation("获取订单详情")
    @GetMapping("/details/{id}")
    public Result<HistoryOrdersVO> OrdersDetail(@PathVariable Long id) {
        log.info("获取订单详情:{}", id);
        HistoryOrdersVO historyOrdersVO = ordersService.ordersDetail(id);
        return Result.success(historyOrdersVO);
    }

    /**
     * 商家取消订单
     * @param ordersCancelDTO
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result CancelOrders(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单:{}", ordersCancelDTO);
        ordersService.adminCancelOrders(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 商家接单
     * @param ordersDTO
     * @return
     */
    @ApiOperation("商家接单")
    @PutMapping("/confirm")
    public Result ConfirmOrders(@RequestBody OrdersDTO ordersDTO) {
        log.info("商家接单:{}", ordersDTO);
        ordersService.adminConfirmOrders(ordersDTO);
        return Result.success();
    }

    /**
     * 商家派送
     * @param id
     * @return
     */
    @ApiOperation("商家派送")
    @PutMapping("/delivery/{id}")
    public Result DeliveryOrders(@PathVariable Long id) {
        log.info("商家派送:{}", id);
        ordersService.adminDeliveryOrders(id);
        return Result.success();
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @ApiOperation("各个状态的订单数量统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> Statistics() {
        log.info("各个状态的订单数量统计:" );
        OrderStatisticsVO orderOverViewVO = ordersService.adminStatistics();
        return Result.success(orderOverViewVO);
    }

    /**
     * 商家拒单
     * @param ordersRejectionDTO
     * @return
     */
    @ApiOperation("商家拒单")
    @PutMapping("/rejection")
    public Result RejectionOrders(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("商家拒单:{}", ordersRejectionDTO);
        ordersService.adminRejectionOrders(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 商家完成订单
     * @param id
     * @return
     */
    @ApiOperation("商家完成订单")
    @PutMapping("/complete/{id}")
    public Result CompleteOrders(@PathVariable Long id) {
        log.info("商家完成订单:{}", id);
        ordersService.adminCompleteOrders(id);
        return Result.success();
    }

}
